<?php
$verfiyPostData = verfiyPostData ();
echo json_encode ( $verfiyPostData );
exit ();
function verfiyPostData() {
	if (! $_POST)
		return $result = [ 
				'status' => "700",
				'msg' => "请求不支持" 
		];
	
	if (! isset ( $_POST ['Head'] ) || empty ( $_POST ['Head'] ))
		return $result = [ 
				'status' => "7001",
				'msg' => "请求数据非法1" 
		];
	
	if (! isset ( $_POST ['Request'] ) || empty ( $_POST ['Request'] ))
		return $result = [ 
				'status' => "702",
				'msg' => "请求数据非法2" 
		];
	
	$Request = json_decode ( base64_decode ( $_POST ['Request'] ), true );
	if (isset ( $Request ['RGuid'] ) && $Request ['RGuid'] == "users.login.code") {
		session_start ();
		if (! isset ( $_SESSION ['codeImage'] ) || empty ( $_SESSION ['codeImage'] ))
			return $result = [ 
					'status' => "703",
					'msg' => "验证码已经失效" 
			];
		
		$Content = json_decode ( base64_decode ( $Request ['Content'] ), true );
		if (! isset ( $Content ['codeVerify'] ) || empty ( $Content ['codeVerify'] ))
			return $result = [ 
					'status' => "704",
					'msg' => "图形验证码为空" 
			];
		if ($Content ['codeVerify'] != $_SESSION ['codeImage'])
			return $result = [ 
					'status' => "705",
					'msg' => "图形验证码不正确" 
			];
		session_destroy ();
	}
	
//	$url = "http://gateway.test.xiaoxiangyizhan.com/gateway";
	 $url = "http://gateway.formal.xiangshoubox.com/gateway";
	return requestCurl ( $url, $_REQUEST );
}
function requestCurl($url, $data = array()) {
	$ch = curl_init (); // 初始化CURL句柄
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true ); // 设为TRUE把curl_exec()结果转化为字串，而不是直接输出
	curl_setopt ( $ch, CURLOPT_POST, true );
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
	curl_setopt ( $ch, CURLOPT_URL, $url );
	curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
	$resData = curl_exec ( $ch ); // 执行预定义的CURL\
	curl_close ( $ch );
	return json_decode ( $resData, true );
}
function getCodeImage() {
	$img = imagecreatetruecolor ( 100, 40 );
	$black = imagecolorallocate ( $img, 0x00, 0x00, 0x00 );
	$green = imagecolorallocate ( $img, 0x00, 0xFF, 0x00 );
	$white = imagecolorallocate ( $img, 0xFF, 0xFF, 0xFF );
	imagefill ( $img, 0, 0, $white );
	// 生成随机的验证码
	$code = '';
	for($i = 0; $i < 4; $i ++) { // 4位数的验证码
		$code .= rand ( 0, 9 );
	}
	imagestring ( $img, 5, 10, 10, $code, $black );
	// 加入噪点干扰
	for($i = 0; $i < 50; $i ++) {
		imagesetpixel ( $img, rand ( 0, 100 ), rand ( 0, 100 ), $black ); // imagesetpixel — 画一个单一像素，语法: bool imagesetpixel ( resource $image , int $x , int $y , int $color )
		imagesetpixel ( $img, rand ( 0, 100 ), rand ( 0, 100 ), $green );
	}
	// 输出验证码
	header ( "content-type: image/png" );
	imagepng ( $img ); // 以 PNG 格式将图像输出到浏览器或文件
	imagedestroy ( $img );
}
